package com.linsen.core.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.experimental.UtilityClass;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/** 省市区信息映射 */
@UtilityClass
public final class IdcardUtil {
    /** 省份信息 */
    public volatile Map<String,String> provinceMap = new HashMap<>();
    /** 省份下辖城市列表 */
    public volatile Map<String, Set<String>> provinceCityMap = new HashMap<>();
    /** 城市所属省份 */
    public volatile Map<String,String> cityProvinceMap = new HashMap<>();
    /** 城市信息 */
    public volatile Map<String,String> cityMap = new HashMap<>();
    /** 城市下辖区县列表 */
    public volatile Map<String,Set<String>> cityCountyMap = new HashMap<>();
    /** 区县所属城市 */
    public volatile Map<String,String> countyCityMap = new HashMap<>();
    /** 区县信息 */
    public volatile Map<String,String> countyMap = new HashMap<>();

    static {
        //初始化省市区信息映射
        try {
            //打包部署后资源文件都被打包到jar中，无法通过路径形式获取，只能通过classpath
//            InputStream inputStream = IdcardUtil.class.getResourceAsStream("/idcard-pcc.json");
            InputStream inputStream = new ClassPathResource("/idcard-pcc.json").getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();

            String str;
            while ((str = bufferedReader.readLine()) != null){
                stringBuilder.append(str + "\n");
            }

            String content = stringBuilder.toString();

            JSONObject jsonObject = JSONObject.parseObject(content);
            JSONObject provinceObj = jsonObject.getJSONObject("86");

            if (ObjectUtil.isNotNull(provinceObj)){
                Set<Map.Entry<String, Object>> provinceEntrySet = provinceObj.entrySet();
                provinceEntrySet.forEach(provinceEntry -> {
                    String provinceCode = provinceEntry.getKey();
                    String provinceName = (String) provinceEntry.getValue();
                    provinceMap.put(provinceCode, provinceName);

                    JSONObject cityObj = jsonObject.getJSONObject(provinceCode);
                    if (ObjectUtil.isNotNull(cityObj)){
                        provinceCityMap.put(provinceCode,cityObj.keySet());
                        Set<Map.Entry<String, Object>> cityEntrySet = cityObj.entrySet();
                        cityEntrySet.forEach(cityEntry -> {
                            String cityCode = cityEntry.getKey();
                            String cityName = (String) cityEntry.getValue();
                            cityProvinceMap.put(cityCode,provinceCode);
                            cityMap.put(cityCode, cityName);

                            JSONObject countyObj = jsonObject.getJSONObject(cityCode);
                            if (ObjectUtil.isNotNull(countyObj)){
                                cityCountyMap.put(cityCode,countyObj.keySet());
                                Set<Map.Entry<String, Object>> countyEntrySet = countyObj.entrySet();
                                countyEntrySet.forEach(countyEntry -> {
                                    String countyCode = countyEntry.getKey();
                                    String countyName = (String) countyEntry.getValue();
                                    countyCityMap.put(countyCode,cityCode);
                                    countyMap.put(countyCode, countyName);
                                });
                            }
                        });
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Description 获取籍贯信息
     * @param code 身份证前6位
     * @Return
     * @Exception
     * @Author zhaoyong
     * @Email zy765203718@gmail.com
     * @Date 2023/3/15 17:17
     */
    public String getNativePlace(String code){
        String nativePlace = "";

        if (ObjectUtil.isNotEmpty(code)){
            //先检查是否匹配区县
            String county = countyMap.get(code);
            if (ObjectUtil.isNotEmpty(county)){
                String cityCode = countyCityMap.get(code);
                String provinceCode = cityProvinceMap.get(cityCode);

                nativePlace += provinceMap.get(provinceCode);
                nativePlace += cityMap.get(cityCode);
                nativePlace += countyMap.get(code);
            }else{
                //否则检查是否匹配城市
                String city = cityMap.get(code);
                if (ObjectUtil.isNotEmpty(city)){
                    String provinceCode = cityProvinceMap.get(code);

                    nativePlace += provinceMap.get(provinceCode);
                    nativePlace += cityMap.get(code);
                }else{
                    //否则检查是否匹配省份
                    String province = provinceMap.get(code);
                    if (ObjectUtil.isNotEmpty(province)){
                        nativePlace += province;
                    }
                }
            }
        }

        return nativePlace;
    }
}
